﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{
    public class newClassDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int newClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from newClass where newClassId=@newClassId");
            htSyn.Add("newClassId", newClassId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得新闻类别列表
        /// <summary>
        /// 获得新闻类别列表
        /// </summary>
        /// <returns></returns>
        public IList<newClass> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select newClassId,newClassName,newClassInfo,newTag ");
            strSql.Append(" FROM newClass ");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据新闻类别号查询实体
        /// <summary>
        /// 根据新闻类别号查询实体
        /// </summary>
        /// <param name="newClassId"></param>
        /// <returns></returns>
        public newClass GetModel(int newClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select newClassId,newClassName,newClassInfo,newTag from newClass ");
            strSql.Append(" where newClassId=@newClassId ");
            htSyn.Add("@newClassId", newClassId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            newClass newclass = new newClass();
            if (dt.Rows.Count>0)
            {
                newclass.newClassId = Convert.ToInt32(dt.Rows[0]["newClassId"]);
                newclass.newClassName = dt.Rows[0]["newClassName"].ToString();
                newclass.newClassInfo = dt.Rows[0]["newClassInfo"].ToString();
                newclass.newTag = dt.Rows[0]["newTag"].ToString();
            }
            return newclass;
        }
        #endregion

        #region 修改新闻类别信息
        /// <summary>
        /// 修改新闻类别信息
        /// </summary>
        /// <param name="newClass"></param>
        /// <returns></returns>
        public bool Update(newClass newClass)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update newClass set ");
            strSql.Append("newClassName=@newClassName,");
            strSql.Append("newClassInfo=@newClassInfo,");
            strSql.Append("newTag=@newTag");
            strSql.Append(" where newClassId=@newClassId ");
            htSyn.Add("newClassName", newClass.newClassName);
            htSyn.Add("newClassInfo", newClass.newClassInfo);
            htSyn.Add("newTag", newClass.newTag);
            htSyn.Add("newClassId", newClass.newClassId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除新闻类别根据新闻类别号
        /// <summary>
        /// 删除新闻类别根据新闻类别号
        /// </summary>
        /// <param name="newClassId"></param>
        /// <returns></returns>
        public bool Delete(int newClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from newClass where newClassId=@newClassId");
            htSyn.Add("newClassId", newClassId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加新闻类别
        /// <summary>
        /// 添加新闻类别
        /// </summary>
        /// <param name="newClass"></param>
        /// <returns></returns>
        public bool Insert(newClass newClass)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into newClass([newClassName],[newClassInfo],[newTag]) values (@newClassName,@newClassInfo,@newTag)");
            htSyn.Add("newClassName", newClass.newClassName);
            htSyn.Add("newClassInfo", newClass.newClassInfo);
            htSyn.Add("newTag", newClass.newTag);
            htSyn.Add("newClassId", newClass.newClassId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<newClass> getProductsBySql(string strSql)
        {
            IList<newClass> list = new List<newClass>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    newClass newclass = new newClass();
                    newclass.newClassId = Convert.ToInt32(dr["newClassId"]);
                    newclass.newClassName = dr["newClassName"].ToString();
                    newclass.newClassInfo = dr["newClassInfo"].ToString();
                    newclass.newTag = dr["newTag"].ToString();
                    list.Add(newclass);
                }
            }
            return list;
        }
        #endregion

        #region 批量删除新闻类别根据新闻类别号---郑同飞
        /// <summary>
        /// 批量删除新闻类别根据新闻类别号---郑同飞
        /// </summary>
        /// <param name="ncId"></param>
        /// <returns></returns>
        public bool Delete(string[] ncId) 
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from newClass where");
            foreach (string newId in ncId)
            {
                strSql.AppendFormat(" newClassId={0} or", newId);
            }
            strSql = strSql.Remove(strSql.Length-1,1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString())>0)
            {
                return true;
            }
            return false;
        }
        #endregion

    }
}
